#include"callback.h"
#include"grid.h"

void DynamicQuadCallbackForCells::update( osg::NodeVisitor*,
osg::Drawable* drawable )
{	
	//Grid_->mass_pcells[5][5]->FLAG_need_callback = true;	
	osg::Geometry* quad = static_cast<osg::Geometry*>( drawable );
	if ( !quad ) return;
	osg::Vec3Array* vertices = static_cast<osg::Vec3Array*>(
	quad->getVertexArray() );
	//Grid_->mass_pcells[5][5]->T_start -= 0.00000001f; 
	if ( !vertices ) return;
	osg::Vec4Array* colors = static_cast<osg::Vec4Array*>(
	quad->getColorArray() );
	if ( !vertices ) return;
	int coord_x = 1, coord_y = 1;
	quad->getUserValue( "coord_x", coord_x );
	quad->getUserValue( "coord_y", coord_y );
	//printf("%i %i\n", coord_x, coord_y );	
	if( !Grid_->mass_pcells[coord_x][coord_y] ) {
		colors->push_back( osg::Vec4(50.0f, 50.0f, 50.0f, 1.0f) );
		quad->dirtyDisplayList();
		quad->dirtyBound();
		return;
	}
	if( !Grid_->mass_pcells[coord_x][coord_y]->FLAG_need_callback )
		return;	
	double red_from_green_part;

	red_from_green_part = 
		( Grid_->mass_pcells[coord_x][coord_y]->T_start - Grid_->T_2 ) /
		( Grid_->T_1 - Grid_->T_2 );
	colors->clear();
	colors->push_back( osg::Vec4( red_from_green_part * 255.0f,
		( 1.0f - red_from_green_part ) * 255.0f, 0.0f, 1.0f) );
	

	//osg::Quat quat(osg::PI*0.01, osg::X_AXIS);
	//vertices->back() = quat * vertices->back();
	quad->dirtyDisplayList();
	quad->dirtyBound();
}

